package Diagnosis

import (
	"MedicalTreatment/global"
	model "MedicalTreatment/model"
	"fmt"
	"github.com/gin-gonic/gin"
)

type QueryResult struct {
	DoctorName  string
	PatientName string
	IdCard      string
	Department  int
}

func DiaSel(r *gin.Engine) {
	r.POST("/diagnosisSelect", func(context *gin.Context) {
		var query QueryResult
		if err := context.ShouldBindJSON(&query); err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		fmt.Println(query)
		term := global.DB.Model(model.Result{})
		if query.IdCard != "" {
			term.Where("patient_idcard  LIKE ?", query.IdCard+"%")
		}
		if query.Department != 0 {
			term.Where("department = ?", query.Department)
		}
		if query.DoctorName != "" {
			term.Where("doctor_name LIKE ?", query.DoctorName+"%")
		}
		if query.PatientName != "" {
			term.Where("patient_name LIKE ?", query.PatientName+"%")
		}
		var diagnosis []model.Result
		if err := term.Debug().Find(&diagnosis).Error; err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
		} else {
			context.JSON(200, gin.H{
				"Msg": diagnosis,
			})
		}
	})
}
